<?php

namespace App\Console\Commands;

use App\model\Area;
use App\model\Coustomer;
use App\model\Industry;
use App\model\Source;
use App\Repository\CustomerRepository;
use App\shiyou\Form;
use App\shiyou\Form1;
use App\shiyou\HdYhq;
use App\shiyou\HdYhq1;
use Illuminate\Console\Command;

class ShiyouMobile extends Command
{
    protected $project_id=15;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'shiyou:mobile {form}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $customerRespository= new CustomerRepository();

        switch ($this->argument('form')){
            case  1:
                $form=HdYhq::query();
                break;
            case 2:
                $form=HdYhq1::query();
                break;
        }
        $form->chunkById(100,function($forms)use($customerRespository){
            foreach ($forms as $v){
                $coustomer=Coustomer::whereMobile($v->mobilee)
                    ->where('created_at',\Illuminate\Support\Carbon::createFromTimestamp($v->datetime)->toDateTimeString())
                    ->where('project_id',$this->project_id)
                    ->first();
                if ($coustomer){
               //     $coustomer->created_at=\Illuminate\Support\Carbon::createFromTimestamp($v->datetime);
               //     $coustomer->save();
                    $this->info($coustomer->customer_id);

                }
                if (!$coustomer){
                    $coustomer=new Coustomer();
                    $coustomer->name=$v->name ?? '未命名';
                    $coustomer->mobile=$v->mobilee;
                    $coustomer->project_id=$this->project_id;
                    $scource=Source::firstOrCreate(['name'=>'世友自建站']);
                    $coustomer->created_at=\Illuminate\Support\Carbon::createFromTimestamp($v->datetime);
                    $coustomer->source_id=$scource->source_id;
                    $coustomer->save();
                    $customerRespository->sendXiaoshou($coustomer);
                }
            }

        });
    }
}
